package com.qf.contorller;

import com.alibaba.fastjson.JSON;
import com.qf.pojo.Type;
import com.qf.service.TypeService;
import com.qf.service.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author LiXuekai on 2022/5/24
 */
@WebServlet("/type")
public class TypeController extends BaseServlet {
    /**
     * 查询所有的商品分类信息  展示
     * @param request
     * @param response
     * @return
     */
    public String findAllType(HttpServletRequest request, HttpServletResponse response){
        //1.调用service业务层 查询所有商品的分类数据
        TypeService typeService=new TypeServiceImpl();
        List<Type> list = typeService.findAllType();
        for (Type type : list) {
            System.out.println(type.toString());
        }
        //2.将查询到的数据保存到requset域中保存  用来携带到页面中
        request.setAttribute("goodsTypeList", list);
        //3.使用转发 跳转到商品分类展示页面
        return "forward:admin/showGoods.jsp";

    }

    /**
     * 商品添加下拉框余姚的分类数据
     * @param request
     * @param response
     */
    public void findAll(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //1.调用service业务层  查询所有商品的分类数据
        TypeService typeService=new TypeServiceImpl();
        List<Type> list = typeService.findAllType();
        //2.将商品集合对象 转换成json格式字符串
        String jsonStr= JSON.toJSONString(list);
        //3.通过流将json格式字符串写会到页面中
        response.getWriter().println(jsonStr);
    }
}